package com.fenghongzhang.day004rxjava;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.ArrayList;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private RxPermissions rxPermissions = new RxPermissions(this);
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rxPermissions.request(
                 Manifest.permission.READ_EXTERNAL_STORAGE,
                 Manifest.permission.WRITE_EXTERNAL_STORAGE,
                 Manifest.permission.CALL_PHONE,
                 Manifest.permission.INTERNET)
           .subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.i(TAG, "accept: "+aBoolean);
            }
        });

        Retrofit build = new Retrofit.Builder()
                .baseUrl("http://117.51.155.210/")
                .addConverterFactory(GsonConverterFactory.create())
                //添加log拦截器
                .client(new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build())
                .build();
        Api api = build.create(Api.class);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), new File("/sdcard/1.jpg"));

        MultipartBody multipartBody = new MultipartBody.Builder()
                .addFormDataPart("file", "hah.jpg", requestBody)
                .setType(MultipartBody.FORM)
                .build();
        Call<ResponseBody> call = api.uploadFile(multipartBody.parts());


        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

//                ResultEntity resultEntity = response.body();
//                String text = resultEntity.getResults().get(0).getValues().getText();
//                Toast.makeText(MainActivity.this, ""+text, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });



//        Retrofit build = new Retrofit.Builder()
//                //添加日志的
//                .client(new OkHttpClient.Builder()
//                        .addInterceptor(new HttpLoggingInterceptor()
//                                .setLevel(HttpLoggingInterceptor.Level.BODY)).build())
//                //基础url和api中的方法拼接
//                .baseUrl("http://117.51.155.210/")
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .build();
//
//        //rxjava结合
//        Api api = build.create(Api.class);
//
//        File externalStorageDirectory = Environment.getExternalStorageDirectory();
//        Log.i(TAG, "onCreate: "+externalStorageDirectory);
//        final RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), new File(externalStorageDirectory,"11.jpg"));
//
//        MultipartBody multipartBody = new MultipartBody.Builder()
//                .addFormDataPart("file", "11.jpg", requestBody)
//                .setType(MultipartBody.FORM)
//                .build();
////
////        api.uploadFile(multipartBody.parts());
//
//
//        Call<ResponseBody> call = api.uploadFile(multipartBody.parts());
//
//
//        call.enqueue(new Callback<ResponseBody>() {
//            @Override
//            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//
//                Log.i(TAG, "onResponse: ");
//
//            }
//
//            @Override
//            public void onFailure(Call<ResponseBody> call, Throwable t) {
//
//            }
//        });


//        api.login("1704","bawei")
//                .subscribeOn(Schedulers.io())//子线
//                .observeOn(AndroidSchedulers.mainThread())//主线
//                .subscribe(new Consumer<User>() {
//                    @Override
//                    public void accept(User user) throws Exception {
//                        Log.i(TAG, "accept: "+user.getMessage());
//                    }
//                });





//        api.getFood4("ios")
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<Food>() {
//                    @Override
//                    public void accept(Food food) throws Exception {
//                        Log.i(TAG, "accept: "+food.getData().size());
//                    }
//                });


//        api.getFood2("ios",2)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<Food>() {
//                    @Override
//                    public void accept(Food food) throws Exception {
//                        Log.i(TAG, "accept: "+food.getData().size());
//                    }
//                });


//        api.getFood1()
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<Food>() {
//                    @Override
//                    public void accept(Food food) throws Exception {
//                        Log.i(TAG, "accept: "+food.getData().size());
//                    }
//                });



        //接口
//        Api api = build.create(Api.class);
//        Call<Food> food = api.getFood();
//
//        food.enqueue(new Callback<Food>() {
//            @Override
//            public void onResponse(Call<Food> call, Response<Food> response) {
//                Log.i(TAG, "onResponse: "+response.body().getData().size());
//            }
//
//            @Override
//            public void onFailure(Call<Food> call, Throwable t) {
//
//            }
//        });


    }

    private void test6() {
        Observable.just("1","2","3")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    Disposable disposable;
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(String s) {
                        if(!disposable.isDisposed()){
                            Log.i(TAG, "onNext: "+s);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        //解除订阅
                        disposable.dispose();


                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void test5() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        Observable.fromIterable(strings)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Log.i(TAG, "accept: "+s);
                    }
                });
    }

    private void test4() {
        Observable.just("1","2","3")
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Log.i(TAG, "accept: "+s);
                    }
                });
    }

    private void test3() {
        Observable.just("1","2","3")
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        Log.i(TAG, "onNext: ");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void test2() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
//                emitter.onError(new RuntimeException("哈哈"));

                emitter.onNext("1");
                emitter.onNext("2");
                emitter.onNext("3");
                emitter.onComplete();
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
                Log.i(TAG, "onNext: "+s);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void test1() {
        //创建被观察者.
        Observable<String> stringObservable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
//                emitter.onError(new RuntimeException("哈哈"));

                emitter.onNext("1");
                emitter.onNext("2");
                emitter.onNext("3");
                emitter.onComplete();
            }
        });

        //创建一个观察者.
        Observer<String> stringObserver = new Observer<String>(){
            //订阅 的时候调用
            @Override
            public void onSubscribe(Disposable d) {
                Log.i(TAG, "onSubscribe: ");
            }

            //next
            @Override
            public void onNext(String s) {
                Log.i(TAG, "onNext: "+s);
            }

            //erroe
            @Override
            public void onError(Throwable e) {

            }

            //complete
            @Override
            public void onComplete() {
                Log.i(TAG, "onComplete: ");
            }
        };

        //订阅
        stringObservable.subscribe(stringObserver);
    }
}
